/*
 * @Author: 詹真琦(legendryztachi@gmail.com)
 * @Date: 2021-11-05 15:37:09
 * @LastEditors: 詹真琦(legendryztachi@gmail.com)
 * @LastEditTime: 2022-04-14 16:53:56
 * @FilePath: \code\cwp-front\pc-collaboration\src\handles\common\print\fragment\opinion-turn.f.js
 * @Description: 意见转发。入参第一个统一为baseInfo的store
 */
import { createForwardList } from '../helper';
import api from '@/api';

export default (useBaseInfo, option) => {
  let forwardList = useBaseInfo.print.turnCommentData; //这里到时候传一个装最外层 转发意见 的数组
  if (option && !forwardList?.length) {
    var zwIframe = useBaseInfo.baseInfo.zwIframe.mainbody;
    var affair = useBaseInfo.baseInfo.businessData.affair;
    api
      .commentGetList({
        moduleId: zwIframe.moduleId,
        affairId: affair.id,
        moduleType: zwIframe.moduleType,
        offset: 1,
        limit: 10000,
        queryType: 1
      })
      .then((res) => {
        const { commentData } = res;
        let list = [];
        commentData.forEach((d) => {
          //获取当前索引
          const index = d.forwardNum - 1;
          //如果已经有这个大块了，就把里面评论添加进去
          if (list[index]) {
            list[index].comments.push(...d.comments);
          } else {
            //否则添加整个大块
            list.push(d);
          }
        });
        forwardList = list;
        if (!forwardList?.length) {
          const printForwardFragment = new PrintFragment(
            $.i18n('collaboration.colPrint.oldOpinion'), // 国际化，标题
            ''
          );
          option.callback(printForwardFragment);
        } else {
          const printForwardFragment = new PrintFragment(
            $.i18n('collaboration.colPrint.oldOpinion'), // 国际化，标题
            createForwardList(forwardList)
          );
          // 这里特殊处理， _doPrint方法里面会调用
          printForwardFragment.isCommentFrame = true;
          option.callback(printForwardFragment);
        }
      });
  } else {
    if (!forwardList?.length) return;

    const printForwardFragment = new PrintFragment(
      $.i18n('collaboration.colPrint.oldOpinion'), // 国际化，标题
      createForwardList(forwardList)
    );
    // 这里特殊处理， _doPrint方法里面会调用
    printForwardFragment.isCommentFrame = true;
    return printForwardFragment;
  }
};
